capture noisily mkdir  "${results}/rdgraphs"

use ${nc_clean_data}/combined_full_wide if dbm==11 | dbm==12 | dbm==1 | dbm==2 , clear

global bins = "2"
global wdays= "20"
global donuts = "8"
local q = ""
global g_ming5 = "& min_g<=5"

foreach y in indexf_testgradsusp index_test ever_exitgrad ever_susp  {
	foreach grade in ming5  {
			foreach cyrs in 1993_1998 {
					foreach w of global wdays {
						foreach b of global bins {
							foreach d of global donuts {
								foreach samp in r_frl {
									local restriction = "${cyrs_`cyrs'} ${`samp'} ${g_`grade'} "
									disp " "
									disp " "
									disp "********************* Y = `y' ***************************"
									disp "*Age: `age'"
									disp "*Cut Years: `cyrs'"
									disp "*Sample: `samp'"
									disp "*Bandwidth: +/- `w' days"
									disp "*Bins: `b' days"
									disp "*Restriction: `restriction' "
									count if `restriction'
									disp "***************************************************************"

									capture drop y
									gen y = `y'
									capture drop predlin_y predlin_se
									capture drop linci_u95 linci_l95

									`q' reg y  i.belowcut##c.z if `restriction'  & inrange(z,-`w'-`d',`w'+`d'-1)& !inrange(z,-`d',`d'-1), cluster(z)

									`q' predict predlin_y if `restriction', xb
									`q' predict predlin_se if `restriction', stdp

									`q' gen linci_u95 = predlin_y+1.96*predlin_se if `restriction'
									`q' gen linci_l95 = predlin_y-1.96*predlin_se if `restriction'

									noisily capture drop bin_z
									noisily capture drop pr_y n_y

									`q' sort z

									`q' gen bin_z=z-mod(z+`d',`b')+`b'/2  if `restriction' & z<0
									`q' replace bin_z=z-mod(z-`d',`b')+`b'/2  if `restriction' & z>=0

									`q' egen pr_y=mean(y) if `restriction', by(bin_z)
									`q' egen n_y=count(y) if `restriction', by(bin_z)

									`q' summ n_y if `restriction'
									local bin_min_n = r(min)

									`q' capture noisily table bin_z if inrange(z,-30,30) & `restriction' , contents(max z min z)

									**Graph Formatting Setup**
									`q' summ pr_y if `restriction'

									`q' local ymin = floor(r(min)*20)/20-0.05
									`q' local ymax = `ymin'+.3
									disp "*"
									`q' local ylab1= `ymin'+(`ymax'-`ymin')/2
									`q' local ylab2= `ymax'

									`q' set scheme s1manual
									sort z
									disp "**"

								 graph twoway rarea `g'ci_u95 `g'ci_l95 z if inrange(z,-`w'-`d',-.0001) & `restriction', color(gs13)  ///
										|| rarea `g'ci_u95 `g'ci_l95 z if inrange(z,0,`w'+`d'-1) & `restriction', color(gs13)  ///
										|| scatter pr_y bin_z if  inrange(z,-`w'-`d',`w'+`d'-1)& !inrange(z,-`d',`d'-1) &`restriction', mcolor(black) msymbol(O) ///
										|| line pred`g'_y z if inrange(z,-`w'-`d',-.0001)  & `restriction', color(black) ///
										|| line pred`g'_y z if inrange(z,0,`w'+`d'-1)  & `restriction', color(black) ///
										xline(0,  lcolor(gs3) lpattern(dash) ) ///
										xline(-8.5,  lcolor(gs11) lpattern(dash) ) ///
										xline(7.5,  lcolor(gs11) lpattern(dash) ) ///
										xlabel(#6) ///
										ylabel(#6) yscale(range(-.2 .1)) /// //`ymin' `ylab1' `ylab2'
										xtitle("Birthdate (Centered at Jan 1)") ytitle("") legend(off) ///

									disp "`g' graph complete"
									graph export ${results}/rdgraphs/nc_`y'.pdf, replace as(pdf)
						}
					}
				}
			}
		}
	}
}
